﻿using UnityEngine;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions; 

public class CSVReader {

    internal static SteveNames GetSurnames(TextAsset fileSurnames)
    {
        string[] surnames = SplitCsvSurname(fileSurnames.text);
        return new SteveNames(surnames);
    }

    public static SteveNames GetNames(TextAsset fileNames)
    {
        string[][] names = SplitCsvGrid(fileNames.text);
        return new SteveNames(names);
    }

    private static string[] SplitCsvSurname(string csvText)
    {
        string[] lines = Regex.Split(csvText, "\r\n");
        return lines;
    }

    private static string[][] SplitCsvGrid(string csvText)
    {
        string[] lines = csvText.Split("\n"[0]);
        int size = lines.Length;
        string[][] names = new string[size][];
        
        for (int i = 0; i < size; i++)
            names[i] = SplitCSVLine(lines[i]);

        return names;
    }

    private static string[] SplitCSVLine(string line)
    {
        string[] row = line.Split(',');

        //elimina i possibili valori di fine riga
        char[] endChar = new char[] { '\r', '\n', ' ' };
        row[row.Length - 1] = row[row.Length - 1].Trim(endChar);
        
        return row;
    }   


    
}
